package com.zhugang.week01;

/**
 * @program algorithms
 * @description: masterMind
 * @author: chanzhugang
 * @create: 2022/04/07 07:33
 */
public class MasterMind {

    public static void main(String[] args) {
        MasterMind mind = new MasterMind();
        String solution = "RGBY";
        String guess = "RGBY";
        int[] result = mind.masterMind(solution, guess);
        for (int i : result) {
            System.out.println(i);
        }
    }

    /**
     * 珠玑妙算： solution = "RGBY", guess = "GGRR", 返回[1,1]
     * 槽位颜色猜对为一次猜中，只猜对颜色但槽位错了为伪猜中
     * 猜中不能算伪猜中
     */
    public int[] masterMind(String solution, String guess) {
        // 先计算猜中的， 再计算伪猜中的， [] guess记录猜中的字符，[] used solution中哪些字符已经被匹配用掉了
        int n = solution.length();
        boolean[] hited = new boolean[n];
        boolean[] used = new boolean[n];
        int hitCount = 0;
        for (int i = 0; i < n; i++) {
            if (solution.charAt(i) == guess.charAt(i)) {
                hitCount++;
                hited[i] = true;
                used[i] = true;
            }
        }

        int fakeHitCount = 0;
        for (int i = 0; i < n; i++) {
            if (hited[i]) {
                continue;
            }
            // 拿每个guess中字符到solution中查找
            for (int j = 0; j < n; j++) {
                if (solution.charAt(j) == guess.charAt(i) && !used[j]) {
                    used[j] = true;
                    fakeHitCount++;
                    break;
                }
            }
        }
        return new int[]{hitCount, fakeHitCount};
    }
}